package org.theSeed.base.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.base.Preconditions;
import org.theSeed.base.exp.BuzExp;
import org.theSeed.base.pojo.sysEnum.SysCode;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 参数校验工具
 */
public class ParamUtil {
    /**
     * 不为null
     * @param getValue
     * @param msg
     */
    public static <T,V> void isNotNull(T obj,Function<T,V> getValue,String msg){
        Preconditions.checkArgument(ObjectUtil.isNotNull(getValue.apply(obj)),msg);
    }

    /**
     * 不为空
     * @param getValue
     * @param msg
     */
    public static <T,V> void isNotEmpty(T obj,Function<T,V> getValue,String msg){
        Preconditions.checkArgument(ObjectUtil.isNotEmpty(getValue.apply(obj)),msg);
    }


    /**
     * 参数校验对象
     */
    public static  class CheckObj <T>{
        //校验对象
        private T checkData;

        //错误信息
        private List<String> msgList = new ArrayList<>();

        public CheckObj() {
        }

        public CheckObj(T checkData) {
            this.checkData = checkData;
        }

        //检查
        public CheckObj check(boolean predicate, String msg){
            try {
                Preconditions.checkArgument(predicate,msg);
            } catch (IllegalArgumentException e) {
                msgList.add(msg);
            }
            return this;
        }

        //异常抛出
        public void finish(){
            if (CollectionUtil.isNotEmpty(msgList)){
                throw new BuzExp(SysCode.PARAM_ERR.getCode(),msgList);
            }
        }
    }
}
